// pages/index/index.js
const app = getApp();
var localData=require("../data/data")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [ ],
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    interval1: 2000,
    duration: 1000, //	滑动动画时长1s
    //功能模块
    //大嘴头条图标
    headlinesImg: "../../assets/icons/toutiao.png",
    //大嘴头条内容
    headLines: [],
    myTrump:[],
    //新品菜
    newGoods: [],
    //经典菜
    classicGoods: [],
  },
 
  changeTo: (event) => {
    console.log(event);
    let where = event.currentTarget.dataset.where;
    console.log(where);
    if (where === "orderFood") {
      wx.switchTab({
        url: `/pages/${where}/${where}`,
      })
    } else {
      wx.navigateTo({
        // queryString 查询字符串 ?
        url: `/pages/class/${where}/${where}`,
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.httpUrl);
    // 加载的时候从easymoc获取全局数据
  //   console.log(localData.classifyList)
  //  this.handlerData(localData.classifyList);
   
    wx.request({
      url: app.globalData.httpUrl+'/api/wx/category',
      complete: res => {
        console.log(res);
        if(res.data.code==0){
          this.handlerData(res);
        }else{
          wx.showToast({
            title: '加载数据失败！',
            icon:'none',
          })
        }
      },
    });
    // this.handlerIndexHome(localData.indexHome);
    wx.request({
      url: app.globalData.httpUrl+'/api/wx/index',
      complete: res => {
        console.log(res);
        this.handlerIndexHome(res);
      }
    });

  },
  // 添加购物车操作
  ordinInCart: function(e){
    console.log(e);
    let id = e.currentTarget.dataset.id;
    let myTrump = this.data.myTrump;
    let carts = app.globalData.carts;
    for(let item of myTrump)
    {
      if (item.id == id ) {
        if (!item.selected) {
          console.log("addsuccess");
          carts.push(item);
          app.globalData.cartTotal++;
          app.globalData.cartTotalPrice += item.retailPrice;
          item.selected = true;
          wx.showToast({
            title: '已添加到购物车',
            icon: "success",
            duration: 750,
          });
        }else if(item.selected){
          app.globalData.carts = carts.filter((cartItem)=>{   //filter返回新数组，所以不能用carts接受，  //不然app.globalData.carts不能改变
             return cartItem.id !=id;
           }); 
          console.log("deletesuccess");
          app.globalData.cartTotal--;
          app.globalData.cartTotalPrice -= item.retailPrice;
          item.selected = false;
        }
      }
    }
    this.setData({
			cart : app.globalData.carts,
			cartTotal: app.globalData.cartTotal,
			cartTotalPrice : app.globalData.cartTotalPrice,
			myTrump: this.data.myTrump,
		});
		app.globalData.indexGoodsInfo.myTrump=this.data.myTrump;
       //将信息加入全局的购物车中
		console.log(app.globalData.carts);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      myTrump: app.globalData.indexGoodsInfo.myTrump,
      newGoods: app.globalData.indexGoodsInfo.newGoods,
      classicGoods: app.globalData.indexGoodsInfo.classicGoods,
      headLines: app.globalData.indexGoodsInfo.headLines,
  });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  handlerData:function(res){
    let newGoodsData=[];
    let classicGoodsData=[];
    let myTrumpData=[];
    const classifyListData=res.data.content;
    app.globalData.classifyList = classifyListData;
    classifyListData.forEach(function(item,index){
      item.children.forEach(function(category){
        category.goodsList.forEach(function(goods){
          goods.p_categoryId=category.parentId;
          if(goods.recommend==2){
            goods.p_categoryId=category.parentId;
            newGoodsData.push(goods);
          }else if(goods.recommend==1){
            classicGoodsData.push(goods);
          }else if(goods.recommend==3){
            myTrumpData.push(goods);
          }
        })
      })
    });
    app.globalData.indexGoodsInfo.newGoods=newGoodsData;
    app.globalData.indexGoodsInfo.classicGoods=classicGoodsData;
    app.globalData.indexGoodsInfo.myTrump=myTrumpData;
    this.setData({
      newGoods: app.globalData.indexGoodsInfo.newGoods,
      classicGoods: app.globalData.indexGoodsInfo.classicGoods,
      myTrump: app.globalData.indexGoodsInfo.myTrump,
      headLines: app.globalData.classifyList.headLines,
      imgUrls:app.globalData.classifyList.homeImageUrls,
    });
  },
  handlerIndexHome:function(res){
    let images=[];
    let adNews=[];
    if(res.data.code==0){
      res.data.content.adHomes.forEach(function(item,index){
        images.push(item.image);
      })
      res.data.content.adNews.forEach(function(item,index){
        adNews.push(item);
      })
      this.setData({
        imgUrls:images,
        headLines:adNews,
      })
    }
  }
})